package com.binvshe.binvshe.organization.tag;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.organization.release.ReleaseEvent;
import com.f2prateek.dart.Dart;
import com.f2prateek.dart.InjectExtra;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/9/6
 */
public class SelectTagFragment extends BaseFragment {
    private static final String KEY_TAGS = "TAGS";
    @Bind(R.id.fllytHasSelect)
    TagFlowLayout mTflytHasSelect;
    @Bind(R.id.fllytMustSelect)
    TagFlowLayout mTflytMustSelect;
    @Bind(R.id.tflytHotTag)
    TagFlowLayout mTflytHotTag;

    private TagAdapter mHotAdapter;  //热门标签
    private TagAdapter mMustSelectAdapter;  //必须选中标签
    private TagAdapter mHasSelectAdapter;


    ArrayList<TagBean> mHotDatas = new ArrayList<>();
    ArrayList<TagBean> mMustDatas = new ArrayList<>();
    @InjectExtra(KEY_TAGS)
    ArrayList<TagBean> mHasSelectDatas = new ArrayList<>();

    public static SelectTagFragment newInstance(ArrayList<TagBean> tags) {
        Bundle args = new Bundle();
        SelectTagFragment fragment = new SelectTagFragment();
        args.putParcelableArrayList(KEY_TAGS, tags);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        Dart.inject(this, getArguments());
        ButterKnife.bind(this, rootView);
        initFlags();
        getCustomToolbar().setVisibility(View.VISIBLE);
        getCustomToolbar().setTitle("选择标签");
        getCustomToolbar().enableBackButton();
        getCustomToolbar().addRightButton("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new ReleaseEvent.TagEvent(mHasSelectDatas));
                getActivity().finish();
            }
        });
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_tagselect, container, false);
    }

    /**
     * 初始化flag 的adapter
     */
    private void initFlags() {
        mHotAdapter = new TagAdapter<TagBean>(mHotDatas) {
            @Override
            public View getView(FlowLayout parent, int position, final TagBean o) {
                View view = getInflater().inflate(R.layout.item_tag, mTflytHotTag, false);
                TextView tvTagDel = (TextView) view.findViewById(R.id.tvTagDel);
                TextView tvTag = (TextView) view.findViewById(R.id.tvTag);
                tvTag.setText(o.getTagContent());
                o.getTagBackGround(tvTag);
                tvTag.setTextColor(o.getTagTextColor());
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (TagBean bean : mHasSelectDatas) {
                            if (TextUtils.equals(bean.getTagContent(), o.getTagContent())) {
                                return;
                            }
                        }
                        mHasSelectDatas.add(o);
                        mHasSelectAdapter.notifyDataChanged();
                    }
                });
                return view;
            }
        };
        mMustSelectAdapter = new TagAdapter<TagBean>(mMustDatas) {
            @Override
            public View getView(FlowLayout parent, int position, final TagBean o) {
                View view = getInflater().inflate(R.layout.item_tag, mTflytMustSelect, false);
                TextView tvTagDel = (TextView) view.findViewById(R.id.tvTagDel);
                TextView tvTag = (TextView) view.findViewById(R.id.tvTag);
                tvTag.setText(o.getTagContent());
                o.getTagBackGround(tvTag);
                tvTag.setTextColor(o.getTagTextColor());
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (TagBean bean : mHasSelectDatas) {
                            if (TextUtils.equals(bean.getTagContent(), o.getTagContent())) {
                                return;
                            }
                        }
                        mHasSelectDatas.add(o);
                        mHasSelectAdapter.notifyDataChanged();
                    }
                });
                return view;
            }
        };
        mHasSelectAdapter = new TagAdapter<TagBean>(mHasSelectDatas) {
            @Override
            public View getView(FlowLayout parent, final int position, TagBean o) {
                View view = getInflater().inflate(R.layout.item_tag, mTflytHasSelect, false);
                TextView tvTagDel = (TextView) view.findViewById(R.id.tvTagDel);
                TextView tvTag = (TextView) view.findViewById(R.id.tvTag);
                tvTagDel.setVisibility(View.VISIBLE);
                tvTagDel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mHasSelectDatas.remove(position);
                        mHasSelectAdapter.notifyDataChanged();
                    }
                });
                tvTag.setText(o.getTagContent());
                o.getTagBackGround(tvTag);
                tvTag.setTextColor(o.getTagTextColor());
                return view;
            }
        };
        mTflytHotTag.setAdapter(mHotAdapter);
        mTflytMustSelect.setAdapter(mMustSelectAdapter);
        mTflytHasSelect.setAdapter(mHasSelectAdapter);
        getHotTags();
        getMustTags();
    }

    /**
     * 获得热门标签数据
     */
    private void getHotTags() {
        IHttpRequest request = new MyHttpRequest(getContext());
        request.get(UrlCenter.GET_HOT_TAG, null, new DataRequestListener<List<TagBean>>(TagBean.class) {
            @Override
            public void onStart(Context context, String url) {
                enableProgress(false);
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(List<TagBean> o) {
                mHotDatas.clear();
                setColor(o);
                mHotDatas.addAll(o);
                mHotAdapter.notifyDataChanged();
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    /**
     * 获得必选标签数据
     */
    private void getMustTags() {
        IHttpRequest request = new MyHttpRequest(getContext());
        request.get(UrlCenter.GET_MUST_TAG, null, new DataRequestListener<List<TagBean>>(TagBean.class) {
            @Override
            public void onStart(Context context, String url) {
                enableProgress(false);
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(List<TagBean> o) {
                mMustDatas.clear();
                setColor(o);
                mMustDatas.addAll(o);
                mMustSelectAdapter.notifyDataChanged();
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    /**
     * 设置tag的色值 默认没色值，务必调用
     *
     * @param data
     */
    private void setColor(List<TagBean> data) {
        for (int i = 0; i < data.size(); i++) {
            data.get(i).setPositon(i);
        }
    }

    private LayoutInflater getInflater() {
        return getLayoutInflater(getArguments());
    }
}
